//
//  ModalViewController.m
//  MapApp
//
//  Created by Student on 3/10/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MapAppModalViewController_iPhone.h"
#import "MapAppBuilding.h"
//#import "MapAppViewController_iPhone.h"


@implementation MapAppModalViewController_iPhone

@synthesize buildingList, delegate;


// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
 if (self) {
 // Custom initialization.
 }
 return self;
 }
 */


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[super viewDidLoad];
	// load our data from a plist file inside our app bundle
	NSString *path = [[NSBundle mainBundle] pathForResource:@"MapAppBuildings" ofType:@"plist"];
	NSArray *buildings = [NSArray arrayWithContentsOfFile:path];
	
	self.buildingList = [[[NSMutableArray alloc] initWithCapacity:[buildings count]] autorelease];
	
	NSDictionary *building; 
	for (building in buildings)
	{
		MapAppBuilding *newBuilding = [[MapAppBuilding alloc] init];
		newBuilding.name = [building objectForKey:@"buildingNameKey"];
		newBuilding.latitude = [building objectForKey:@"latitudeKey"];;
		newBuilding.longitude = [building objectForKey:@"longitudeKey"];
		
		[self.buildingList addObject:newBuilding];
		[newBuilding release];
	}
	
	// Create the cancel button to dismiss the modal list view
	UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(cancel:)];
	self.navigationItem.leftBarButtonItem = item;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}


- (IBAction) cancel:(id)sender{
	[self.navigationController dismissModalViewControllerAnimated:YES];
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.buildingList count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * const kPlacesCellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kPlacesCellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kPlacesCellIdentifier] autorelease];
        cell.editingAccessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    }
    
    MapAppBuilding *mapAppBuilding = [self.buildingList objectAtIndex:indexPath.row];
    cell.textLabel.text = mapAppBuilding.name;
	
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [delegate mapAppModalViewController_iPhone:self didChooseBuilding:[self.buildingList objectAtIndex:indexPath.row]];
    [self.navigationController popViewControllerAnimated:YES];
}



/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations.
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[buildingList release];
    [super dealloc];
}


@end
